﻿using mediaControl.Event;
using mediaControl.Utils;
using mediaControl.Views;
using mediaControl.Vo;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using Application = System.Windows.Application;

namespace mediaControl.ViewModels
{
    public class HomePageViewModel : BindableBase
    {
        //事件聚合器
        private readonly IEventAggregator _eventAggregator;

        private readonly IDialogService _dialogService;

        private IContainerExtension _containerExtension;

        private ProgramVo program;

        private ObservableCollection<string> list = new ObservableCollection<string>();

        public DelegateCommand NewProgramCommand { get; set; }

        public DelegateCommand OpenProgramCommand { get; set; }

        public DelegateCommand<object> DoubleClickCommond { get; set; }
        public ProgramVo Program { get => program; set => program = value; }
        public ObservableCollection<string> List { get { return list; } set { this.list = value; RaisePropertyChanged(); } }

        public HomePageViewModel(IEventAggregator eventAggregator, IDialogService dialogService,IContainerExtension containerExtension)
        {
            _eventAggregator = eventAggregator;
            _dialogService = dialogService;
            _containerExtension = containerExtension;
            NewProgramCommand = new DelegateCommand(newProgram);
            OpenProgramCommand = new DelegateCommand(openProgram);
            DoubleClickCommond = new DelegateCommand<object>(openProgramByHistory);
            List = WriteFileUtil.GetHistoryList();
        }

        private void openProgramByHistory(object obj)
        {
            
            System.Windows.Controls.ListView listView = obj as System.Windows.Controls.ListView;
            string filePath = listView.SelectedItem as string;
            string content = "";//返回的结果字符串
            using (StreamReader sr = new StreamReader(@filePath))
            {
                content = sr.ReadToEnd();//一次性把文本内容读完
            }
            JObject programInfo = JsonConvert.DeserializeObject<JObject>(content);
            Program = JsonConvert.DeserializeObject<ProgramVo>((String)programInfo.GetValue("programInfo"));
            _eventAggregator.GetEvent<OpenMainWindowEvent>().Publish(Program);


        }

        private void openProgram()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择文件";
            openFileDialog.Filter = "(*.info)|*.info";
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string foldPath = openFileDialog.FileName;
                string content = "";//返回的结果字符串
                using (StreamReader sr = new StreamReader(@foldPath))
                {
                    content = sr.ReadToEnd();//一次性把文本内容读完
                }
                JObject programInfo = JsonConvert.DeserializeObject<JObject>(content);
                Program = JsonConvert.DeserializeObject<ProgramVo>((String)programInfo.GetValue("programInfo"));
                //System.Windows.MessageBox.Show(content);
                //显示到列表
                //program.No = 0;
                //eventAggregator.GetEvent<ProgramEvent>().Publish(program);

                //Window mainWindow = _containerExtension.Resolve<Main>();
                //mainWindow.Owner = Application.Current.MainWindow;
                //mainWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                //mainWindow.Title = "嘟答智能播放系统\\" + Program.Name + "\\" + Program.FilePath;
                //mainWindow?.Show();
                //Application.Current.MainWindow = mainWindow;
                _eventAggregator.GetEvent<OpenMainWindowEvent>().Publish(program);
                WriteFileUtil.SaveOpenList(foldPath);
                //System.Windows.Application.Current.MainWindow.Close();

            }
        }

        private void callback(IDialogResult result)
        {
            if (result.Result == ButtonResult.OK)
            {
                Program = result.Parameters.GetValue<ProgramVo>("program");
                _eventAggregator.GetEvent<OpenMainWindowEvent>().Publish(Program);
                //System.Windows.Application.Current.MainWindow.Close();
            }
        }

        private void newProgram()
        {
            IDialogParameters parameters = new DialogParameters();
            _dialogService.ShowDialog("newProgram", parameters, callback);
        }
    }
}
